package com.jxnu.escms.Control.EpidControl;

import com.jxnu.escms.Service.EpidService.EpidService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yihui
 * 主页后台代码
 */
@Slf4j
@RestController
@RequestMapping("/epidemic")
public class EpidemicControl {
    @Resource
    private EpidService epidService;

    @RequestMapping("/all")
    public Map getKeyData(){
        Map keyData = epidService.getKeyData();
        log.info("获取到全国疫情汇总数据" + keyData);
        if(keyData == null){
            return null;
        }
        return keyData;
    }

    @RequestMapping("/add")
    public List<Map<String, Object>> getHistAddData(){
        List<Map<String, Object>> res = epidService.getAddData();
        log.info("得到全国近七天新增数据");
        log.info(String.valueOf(res));
        // 翻转结果
        res.sort(new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                String time1 = (String) o1.get("time");
                String time2 = (String) o2.get("time");
                if(time1.compareTo(time2) > 0){
                    return 0;
                }
                return -1;
            }
        });
        return res;
    }

    @RequestMapping("/now")
    public Map getNowData() throws ParseException {
        log.info("获取新增确诊数前5省份");
        List<Map<String, String>> nowData = epidService.getNowData();
        // 解析时间，转化为yy-MM-dd
        SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        SimpleDateFormat  nowForm = new SimpleDateFormat("yyyy-MM-dd");
        String nowTime = null;
        for(Map<String, String> d : nowData){
            String time = d.get("time");
            nowTime = nowForm.format(simpleFormatter.parse(time));
            d.remove("time");
        }
        // 将时间与其他数据分开
        Map<String, Object> res = new HashMap<>();
        res.put("time", nowTime);
        res.put("data", nowData);

        log.info(String.valueOf(res));
        return res;
    }

    @RequestMapping("/bar")
    public List getBarData(){
        List<Map<String, Object>> barData = epidService.getBarData();
        log.info("江西各个城市的疫情数据");
        log.info(String.valueOf(barData));
        barData.sort(new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                if(Objects.equals((String) o1.get("city"), "境外输入")){
                    return 0;
                }
                return -1;
            }
        });
        return barData;
    }
}
